package spark.core.java;

import org.apache.commons.collections.map.HashedMap;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by Administrator on 2018/2/17/017.
 */
public class ProductData {

    static Map<String,Integer> idMap = new HashMap<String, Integer>();
    static Map<String,Integer> deptnoMap = new HashMap<String, Integer>();
    public static void main(String[] args) {

        System.out.println(getNumRandom(20));
        System.out.println(getNumRandomWeishu(100000));
        System.out.println(getDayRandom());
        productEMPDEPT();
    }


    public static int getNumRandomWeishu(int wei){
       return  (int)((Math.random()*9+1)*wei);
    }

    public static int getNumRandom(int num)
    {
        Random r = new Random();
        return r.nextInt(num);
    }

    public static String getMonthRandom(){
        int month = getNumRandom(12);
        String monthStr = "11";
        if( month !=0 && month < 10){
            monthStr = "0" + month;
        }else if(month !=0 && month > 10 ){
            monthStr = "" + month;
        }
        return monthStr;
    }

    public static String getDayRandom(){
        int month = getNumRandom(29);
        String monthStr = "11";
        if( month !=0 && month < 10){
            monthStr = "0" + month;
        }else if(month !=0 && month > 10 ){
            monthStr = "" + month;
        }
        return monthStr;
    }

    public static void productEMPDEPT(){
        FileWriter empWriter = null;
        FileWriter deptWriter= null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            empWriter = new FileWriter("datas/emp.txt", true);
            deptWriter = new FileWriter("datas/dept.txt", true);

            for(int i=0;i<99999999;i++){
                System.out.println(i);
                int id = getNumRandomWeishu(100000);
                if(null == idMap.get(id+"")){
                    int deptno = getNumRandom(30);
                    String empcontent = id+"\tALLEN\tSALESMAN\t769"+getNumRandom(9)+"\t2017-"+getMonthRandom()+"-"+getDayRandom()+"\t"+getNumRandomWeishu(10000)+".00\t"+getNumRandom(2000)+".00\t"+deptno+"\n";
                    empWriter.write(empcontent);
                    idMap.put(id + "", 1);
                    if(null == deptnoMap.get(""+deptno)){
                        String deptcontent = deptno+"\tACCOUNTING"+deptno+"\tNEW YORK\n";
                        deptWriter.write(deptcontent);
                        deptnoMap.put(""+deptno,1);
                    }
                }
            }



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(empWriter != null){
                    empWriter.close();
                }
                if(deptWriter != null){
                    deptWriter.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
